//
//  CYHomeTableViewController.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CYHomeTableViewController: CYVisitorViewCotroller {

    lazy var statusListViewModel: CYStatusListViewModel = CYStatusListViewModel()
    
//    var statusArray: [CYStatus]?
    
    private let ZjCyr = "Cyrcell"
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !userLogin {
            visitorView.setPageInfo(nil, message: nil)
            return
        }
        setupUI()
        
        loadData()
        
        //监听点击评论按钮的通知
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "aa:", name: "CryZz", object: nil)
 
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    func aa(noti:NSNotification) {
        
        let vc = noti.object as! UINavigationController
        self.presentViewController(vc, animated:true , completion: nil)

        
    }
    
    
    

    private func setupUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_friendsearch", target:nil , action: nil)
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_pop", target: self, action: "pop")
        
        setupTableView()
    }
    func pop() {
        
        let vc = CYTempViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }

   
    private func setupTableView() {
        tableView.registerClass(CYStatusCell.self, forCellReuseIdentifier: ZjCyr)
//        tableView.rowHeight = 60
//        tableView.rowHeight = 250
        //设置自动计算行高
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 300
        
        tableView.separatorStyle = .None
        tableView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        tableView.tableFooterView = pullUpView
        
//        refreshControl = UIRefreshControl()
//        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)

        tableView.addSubview(cyRefreshControl)
        
        cyRefreshControl.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
        
    }
    
   @objc private func loadData() {
        statusListViewModel.loadData(pullUpView.isAnimating(), finished: { (isSuccess) -> () in
            
            if isSuccess {
                self.tableView.reloadData()
            }else {
                print("请求数据失败")
                
            }
            self.endRefresh()
        })
        
        
    }
    
    func endRefresh() {
        pullUpView.stopAnimating()
        refreshControl?.endRefreshing()
    }
    
    private lazy var pullUpView: UIActivityIndicatorView = {
       let indicatiorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicatiorView.color = UIColor.yellowColor()
        
        return indicatiorView
        
    }()
    
    private lazy var cyRefreshControl: CYRefreshControl = CYRefreshControl()
    
}
extension CYHomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.statusArray?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(ZjCyr, forIndexPath: indexPath) as! CYStatusCell
        
//        cell.textLabel?.text = statusArray![indexPath.row].text
//        cell.textLabel?.text = statusListViewModel.statusArray![indexPath.row].user?.screen_name
        cell.statusViewModel = statusListViewModel.statusArray! [indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == statusListViewModel.statusArray!.count - 1 && pullUpView.isAnimating() == false {
            pullUpView.startAnimating()
            self.loadData()
        }
        
    }
    
}
